package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.CarBranHotAdapter;
import com.wqtz.chechai.adapter.CityAdapter;
import com.wqtz.chechai.adapter.SortAdapter;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.base.rebaseadapter.MyHolder;
import com.wqtz.chechai.base.rebaseadapter.RecyclerAdapter;
import com.wqtz.chechai.bean.CarBrandBean;
import com.wqtz.chechai.bean.CarSortModel;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.FileUtils;
import com.wqtz.chechai.utils.JsonUtils;
import com.wqtz.chechai.utils.StringUtils;
import com.wqtz.chechai.utils.ToastUtils;
import com.wqtz.chechai.widget.PinyinComparator;
import com.wqtz.chechai.widget.PinyinUtils;
import com.wqtz.chechai.widget.SideBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/27 11:17
 */

public class CarBrandSelectActivity extends BaseActivity implements RecyclerAdapter.OnItemLickListener {
	@BindView(R.id.iv_back)
	ImageView mIvBack;
	@BindView(R.id.tv_title)
	TextView mTvTitle;
	@BindView(R.id.country_lvcarbrand)
	ListView mCountryLvcarbrand;
	@BindView(R.id.dialog)
	TextView mDialog;
	@BindView(R.id.sidrbar)
	SideBar mSidrbar;
	private SortAdapter adapter;
	private CarBranHotAdapter carhotadapter;
	private List<CarSortModel> sourceDateList;
	CarBrandBean mCarBrandBean ;
	private List<String> mHotCarModel;
	String carhot[];
	private String xname;
	private TextView tv;
	private String[] carhotId;


	@Override
	public void init() {
		ButterKnife.bind(this);
		mCarBrandBean = new CarBrandBean();
		mCarBrandBean =JsonUtils.getCarBrand(FileUtils.getJson(this,"carbrand.json"));
		initData();
		setAdapter();
		initEvents();
	}

	private void setAdapter() {
		Collections.sort(sourceDateList, new PinyinComparator());//根据拼音排序
		adapter = new SortAdapter(this, sourceDateList);
		mCountryLvcarbrand.addHeaderView(initHeadView());
		mCountryLvcarbrand.setAdapter(adapter);
	}

	private void initEvents() {
		//设置右侧触摸监听
		mSidrbar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
			@Override
			public void onTouchingLetterChanged(String s) {
				//该字母首次出现的位置
				int position = adapter.getPositionForSection(s.charAt(0));
				if (position != -1) {
					mCountryLvcarbrand.setSelection(position + 1);
				}
			}
		});

		//ListView的点击事件
		mCountryLvcarbrand.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
			                        int position, long id) {
				//
				mTvTitle.setText(((CarSortModel) adapter.getItem(position)).getName());
				Toast.makeText(getApplication(), ((CarSortModel) adapter.getItem(position)).getName(), Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(CarBrandSelectActivity.this,CarSeriesActivity.class);
				intent.putExtra("brand", ((CarSortModel) adapter.getItem(position)).get_id());
				startActivityForResult(intent,1004); //跳转到下一个页面要求结果
			}
		});
	}

	private void initData() {
		mSidrbar.setTextView(mDialog);
		sourceDateList = new ArrayList<>();
		mHotCarModel = new ArrayList<>();
		carhot = new String[]{"奥迪","宝马","奔驰","本田","丰田","大众","路虎","福特","马自达","保时捷"};
		carhotId = new String[]{"2000172","2000180","2000194","2000195","2000235","2000218","2000284","2000236","2000285","2000185"};
			for (int i = 0; i < mCarBrandBean.getResult().getData().size(); i++) {
			CarSortModel c = new CarSortModel();
			c.setName(mCarBrandBean.getResult().getData().get(i).getBig_ppname());
			c.set_id(mCarBrandBean.getResult().getData().get(i).getId());
			c.setSortLetters(mCarBrandBean.getResult().getData().get(i).getPin());
			sourceDateList.add(c);
		}
		mHotCarModel = new ArrayList<>();
		for (int i = 0; i <carhot.length ; i++) {
			mHotCarModel.add(carhot[i]);
		}
		Logger.e(mHotCarModel.size()+" ");

	}

	private View initHeadView() {
		View  headView = LayoutInflater.from(this).inflate(R.layout.headview,null);
		RecyclerView r = (RecyclerView) headView.findViewById(R.id.rv_hot);
		carhotadapter= new CarBranHotAdapter(R.layout.gridview_item,mHotCarModel);
		r.setLayoutManager(new GridLayoutManager(this,5));
		r.setAdapter(carhotadapter);
		carhotadapter.setOnitemClickListener(this);
		return headView;
	}

	@Override
	public void loadData() {

	}

	@Override
	public int setContentViews() {
		return R.layout.activity_carselector;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}


	@OnClick(R.id.iv_back)
	public void onClick() {
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==1004&&resultCode==RESULT_OK){
			Intent intent = new Intent();
			intent.putExtra("cxname",data.getStringExtra("cxname"));
			intent.putExtra("id",data.getStringExtra("id"));
			intent.putExtra("price",data.getStringExtra("price"));
			setResult(RESULT_OK,intent);
			this.finish();
		}

	}

	@Override
	public void onItemLick(MyHolder holder, int position) {
		tv = (TextView) holder.getView(R.id.tv_carmodel);
		//ToastUtils.show(this,tv.getText().toString(),1000);
		Intent intent = new Intent(CarBrandSelectActivity.this,CarSeriesActivity.class);
		intent.putExtra("brand", carhotId[position]);
		intent.putExtra("carBrand",tv.getText().toString());
		startActivityForResult(intent,1004); //跳转到下一个页面要求结果
	}

	@Override
	public void onLongItemLick(MyHolder holder, int position) {

	}

	@Override
	protected void onResume() {
		super.onResume();
		activitys.add(this); //添加当前的activity
	}
}
